package com.zhuoyue.web.controller;


import com.zhuoyue.biz.core.api.dto.rep.RoomRepDTO;
import com.zhuoyue.biz.core.api.service.impl.RoomService;
import com.zhuoyue.common.aop.auth.CheckLogin;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import org.hibernate.validator.constraints.Length;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.zhuoyue.biz.core.api.domain.RoomDO;
import com.zhuoyue.common.dto.ControllerResult;

import javax.validation.constraints.NotBlank;


/**
 * @author wjming
 */
@RestController
@Scope("prototype")
@RequestMapping("/api/room")
@Validated
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class AppRoomController {

    private final RoomService roomService;

    /**
     * 获取家庭信息
     * 通过家庭ID获取房间信息
     *
     * @param homeId 家庭ID
     * @return
     */
    @GetMapping("/list")
    @CheckLogin
    public ControllerResult getRoomListByHomeId(@NotBlank(message = "家庭ID不能为空") String homeId) {
        return ControllerResult.toReturnDataSuccess("获取成功", roomService.getRoomsByHomeId(homeId));
    }

    /**
     * 添加房间信息
     *
     * @param homeId
     * @param name
     * @param imgUrl
     * @return
     */
    @PostMapping("/add")
    @CheckLogin
    public ControllerResult addRoom(@NotBlank(message = "家庭ID不能为空") String homeId,
                                    @RequestParam @NotBlank(message = "房间名称不能为空") @Length(max = 15, message = "不能超过15个字符") String name,
                                    String imgUrl) {
        RoomDO room = new RoomDO();
        room.setHomeId(homeId);
        room.setName(name);
        room.setImgUrl(StringUtils.isEmpty(imgUrl) ? "default_room" : imgUrl);
        boolean save = roomService.save(room);
        if (!save) {
            return ControllerResult.toParamFail("新增房间失败");
        }
        RoomRepDTO roomRepDTO = new RoomRepDTO();
        roomRepDTO.setDeviceCount(0);
        BeanUtils.copyProperties(room, roomRepDTO);
        roomRepDTO.setCreateTime(room.getCreateTime());
        return ControllerResult.toReturnDataSuccess("添加成功", roomRepDTO);
    }


    /**
     * 修改房间信息
     *
     * @param roomId
     * @param name
     * @param imgUrl
     * @return
     */
    @PutMapping("/modify")
    @CheckLogin
    public ControllerResult updateRoomById(@NotBlank(message = "房间ID不能为空!") String roomId,
                                           String name, String imgUrl) {
        if (StringUtils.isEmpty(name) && StringUtils.isEmpty(imgUrl)) {
            throw new ZyServerException(ErrorType.OBJECT_NOT_FOUND);
        }
        RoomDO room = new RoomDO();
        room.setId(roomId);
        room.setName(name);
        room.setImgUrl(imgUrl);
        roomService.updateRoomById(room);
        return ControllerResult.toReturnMessageSuccess("修改成功");
    }

    /**
     * 删除房间信息
     *
     * @param roomId
     * @return
     */
    @DeleteMapping("/delete")
    @CheckLogin
    public ControllerResult deleteRoomById(@NotBlank(message = "房间ID不能为空!") String roomId) {
        roomService.deleteRoomById(roomId);
        return ControllerResult.toReturnMessageSuccess("删除成功");
    }
}
